﻿using HelixToolkit.Wpf;
using Stylet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Media3D;

namespace Learn3D.Stylet.Pages
{
    public class MainViewModel : Screen
    {
        protected override void OnViewLoaded()
        {
            base.OnViewLoaded();
            LoadGroundModel();
            LoadDynamicModel();
            InitColorBind();
            LoadTextGeometry();
        }

        public Model3D ModelGround { get; set; }
        public void LoadGroundModel()
        {
            // Create some materials       
            var grayMaterial = MaterialHelper.CreateMaterial(Colors.LightGray);
            var yellowMaterial = MaterialHelper.CreateMaterial(Colors.LightYellow);
            var redMaterial = MaterialHelper.CreateMaterial(Colors.Red);
            var greenMaterial = MaterialHelper.CreateMaterial(Colors.Green);
            var blueMaterial = MaterialHelper.CreateMaterial(Colors.Blue);
            var insideMaterial = MaterialHelper.CreateMaterial(Colors.Yellow);

            // Create a model group
            Model3DGroup modelGroup = new Model3DGroup();

            //原点
            MeshBuilder meshBoxBuilder = new MeshBuilder(false, false);
            meshBoxBuilder.AddBox(new Point3D(0, 0, 0), 2, 2, 2);
            MeshGeometry3D meshBox = meshBoxBuilder.ToMesh(true);
            modelGroup.Children.Add(new GeometryModel3D { Geometry = meshBox, Material = yellowMaterial, BackMaterial = insideMaterial });

            //背景模型
            MeshBuilder meshGroundBuilder = new MeshBuilder(false, false);
            meshGroundBuilder.AddBox(new Point3D(0, 0, 0), 100, 80, 2);
            MeshGeometry3D meshGround = meshGroundBuilder.ToMesh(true);

            var groungMaterial = new DiffuseMaterial(new SolidColorBrush(Color.FromArgb(40,0,190,0)));
            var groudInsideMaterial = new DiffuseMaterial(new SolidColorBrush(Color.FromArgb(40, 0, 190, 0)));
            modelGroup.Children.Add(new GeometryModel3D { Geometry = meshGround, Transform = new TranslateTransform3D(0, 0, -3), Material = groungMaterial, BackMaterial = groudInsideMaterial });

            //三轴
            MeshBuilder xGroundBuilder = new MeshBuilder(false, false);
            xGroundBuilder.AddArrow(new Point3D(0, 0, 0), new Point3D(60, 0, 0), 1);
            MeshGeometry3D xGround = xGroundBuilder.ToMesh(true);
            modelGroup.Children.Add(new GeometryModel3D { Geometry = xGround, Material = redMaterial, BackMaterial = insideMaterial });

            MeshBuilder yGroundBuilder = new MeshBuilder(false, false);
            yGroundBuilder.AddArrow(new Point3D(0, 0, 0), new Point3D(0, 60, 0), 1);
            MeshGeometry3D yGround = yGroundBuilder.ToMesh(true);
            modelGroup.Children.Add(new GeometryModel3D { Geometry = yGround, Material = greenMaterial, BackMaterial = insideMaterial });

            MeshBuilder zGroundBuilder = new MeshBuilder(false, false);
            zGroundBuilder.AddArrow(new Point3D(0, 0, 0), new Point3D(0, 0, 50), 1);
            MeshGeometry3D zGround = zGroundBuilder.ToMesh(true);
            modelGroup.Children.Add(new GeometryModel3D { Geometry = zGround, Material = blueMaterial, BackMaterial = insideMaterial });

            this.ModelGround = modelGroup;
        }


        public Model3D ModelDynamic { get; set; }
        public void LoadDynamicModel()
        {
            // Create some materials           
            var redMaterial = MaterialHelper.CreateMaterial(Color.FromRgb(Color_R, Color_G, Color_B));
            var insideMaterial = MaterialHelper.CreateMaterial(Colors.Gray);

            // Create a model group
            Model3DGroup modelDynamic = new Model3DGroup();

            //原点
            MeshBuilder meshBoxBuilder = new MeshBuilder(false, false);
            meshBoxBuilder.AddEllipsoid(new Point3D(20, 20, 10), 5, 5, 5);
            MeshGeometry3D meshBox = meshBoxBuilder.ToMesh(true);
            modelDynamic.Children.Add(new GeometryModel3D { Geometry = meshBox, Material = redMaterial, BackMaterial = insideMaterial });

            this.ModelDynamic = modelDynamic;
        }

        public byte Color_R { get; set; } = 0;
        public byte Color_G { get; set; } = 0;
        public byte Color_B { get; set; } = 0;

        private void InitColorBind()
        {
            this.Bind(s => Color_R, (o, e) => ColorChanged());
            this.Bind(s => Color_G, (o, e) => ColorChanged());
            this.Bind(s => Color_B, (o, e) => ColorChanged());
        }

        public void ColorChanged()
        {
            LoadDynamicModel();
        }


        public MeshGeometry3D TextGeometry { get; set; }
        public void LoadTextGeometry()
        {
            MeshBuilder builder = new MeshBuilder(false, false);
            builder.ExtrudeText("Helix Toolkit", "Arial",
                FontStyles.Normal, FontWeights.Bold, 5,
                new Vector3D(1, 0, 0), new Point3D(-40, -20, 0), new Point3D(-40, -20, 2));
            TextGeometry = builder.ToMesh(true);
        }

        public double TranslateValue { get; set; } = 0;
    }
}
